var params = [];
$(function() {
    $.ajax({
        url: ctx + "/shoppingCart/list.do",
        data: {ids:$('#goodIds').val()},
        success: function (data) {
            var shopId = "";
            var sum=0;
            var total = 0;
            for (var i = 0; i < data.length; i++) {
                total = total + parseInt(data[i].price*100)*data[i].num;
                var temp = sum;
                sum = sum + parseInt(data[i].price*100)*data[i].num;
                if (shopId != data[i].shopId) {
                    if(i!=0){
                        $('.info-tijiao').before('' +
                            '<div class="info-heji">' +
                            '<p class="f-r">店铺合计：<span>¥'+(temp/100)+'</span></p>' +
                            '</div>');
                        sum = data[i].price*100*data[i].num;
                    }
                    shopId = data[i].shopId;
                    params[params.length] = {};
                    params[params.length-1].shopId = shopId;
                    params[params.length-1].goodIds = data[i].shoppingCartId;
                    $('.info-tijiao').before('' +
                        '<div class="cart-con-info">' +
                        '<div class="info-top">'+
                        '<span>商家：' + data[i].shopName + '</span>' +
                        '</div>' +
                        getHtml(shopId, data[i]) +
                        '</div>');
                } else {
                    params[params.length-1].goodIds = params[params.length-1].goodIds+","+data[i].shoppingCartId;
                    $('.info-tijiao').before('' +
                        '<div class="cart-con-info" style="margin-top: 12px;">' +
                        getHtml(shopId, data[i]) +
                        '</div>');
                }
            }
            $('.info-tijiao').before('' +
                '<div class="info-heji">' +
                    '<p class="f-r">店铺合计：<span>¥'+(sum/100)+'</span></p>' +
                '</div>');
            $('#total').html("¥"+(total/100));
        }

    });

    $("#submitOrder").click(function(){
        if($('[name="receiverLi"]').length==0){
            alert("请选择收货人")
            return;
        }
        if(params.length>1){
            confirm('<div style="line-height: 33px;">所选商品来自多个商铺，提交订单后系统将根据店铺生成多个订单，是否继续操作？</div>',submitOrder);
        }else{
            confirm("是否确认提交订单？",submitOrder);
        }
    });
});

function submitOrder(result){
    var receiverId = $('[name="receiverLi"].current [name="receiverId"]').val();
    if(result=="yes"){
        openMask();
        var shopIds = params[0].shopId;
        var goodIds = params[0].goodIds;
        for(var i=1;i<params.length;i++){
            shopIds = shopIds+";"+params[i].shopId;
            goodIds = goodIds+";"+params[i].goodIds;
        }
            $.ajax({
                url:ctx+"/order/add.do",
                data:{
                    shopIds:shopIds,
                    goodIds:goodIds,
                    receiverId:receiverId
                },
                success:function(data){
                    if(data.error){
                        alert("<div style='line-height: 33px;'>错误："+data.error+"</div>");
                        closeMask();
                        return;
                    }
                    if(data.flag==params.length){
                        location.href = ctx+"/user/personalCenter.do?page=orderListForBuyerPage"
                    }else{
                        alert("提交失败");
                    }
                }
            });
    }
}

function getHtml(shopId,good){
    return ''+
        '<div class="info-mid">' +
        '<div class="mid-tu f-l" style="margin-left: 30px;">' +
        '<a><img style="cursor:pointer;" height="80" width="80" src="'+ctx+'/resource/download.do?id=' + good.photoId+'"/></a>' +
        '</div>' +
        '<div class="mid-font f-l">' +
        '<a>'+good.goodName+'</a>' +
        '</div>' +
        '<div class="mid-sl f-l" style="width: 131px;text-align: center;font-size: 13px;">' +
         good.num +
        '</div>' +
        '<p class="mid-dj f-l" id="price'+good.shoppingCartId+'">'+good.price+'</p>' +
        '<p class="mid-je f-l" id="sum'+good.shoppingCartId+'">'+(good.price*100*good.num/100)+'</p>' +
        '<div style="clear:both;"></div>' +
        '</div>';
}

function selectReceiver(li){
    if($(li).hasClass("current"))
        return;
    else{
        $(li).addClass("current");
        $(li).siblings('.current').removeClass("current");
    }
}